#region Usings

using System;
using Karma.Framework.Core.IoC;
using Ninject.Core.Activation;
using Ninject.Core.Creation;
using Karma.Framework.Core.Domain.Tools.Import.Configuration;
using Karma.Framework.Core.Domain.Tools.Import.Data;
using Karma.Framework.Core.Domain.Tools.Import.Extension;

#endregion

namespace Karma.Framework.Core.Domain.Tools.Import.Providers
{
    public class EntityPersisterProvider : SimpleProvider<EntityPersisterBase>
    {
        protected override EntityPersisterBase CreateInstance(IContext context)
        {
            var persister = new DefaultEntityPersister();
            var listeners = ImporterConfiguration.Listerners;
            foreach (var listener in listeners)
            {
                var adapter = new EntityPersisterAdapter((IEntityPersisterListener)IoCFactory.Resolve(listener));
                persister.BeforePersistAll += adapter.OnBeforePersistAll;
                persister.AfterPersistAll += adapter.OnAfterPersistAll;
                persister.BeforePersistEntity += adapter.OnBeforePersistEntity;
                persister.AfterPersistEntity += adapter.OnAfterPersistEntity;
            }

            return persister;
        }
    }
}